package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
)

type User struct {
	Name string
	Age  int
}

func main() {
	router := gin.Default()
	router.Use(m1, m2)
	router.GET("/global_middleware1", func(c *gin.Context) {
		name, ok := c.Get("user")
		if ok {
			fmt.Println(name.(User).Name, name.(User).Age)
		}
		fmt.Println("main1 in")
		c.JSON(http.StatusOK, gin.H{"msg": "success"})
		fmt.Println("main1 out")
	})
	router.GET("/global_middleware2", func(c *gin.Context) {
		fmt.Println("main2 in")
		c.JSON(http.StatusOK, gin.H{"msg": "success"})
		fmt.Println("main2 out")
	})

	router.Run(":8080")
}

func m1(c *gin.Context) {
	fmt.Println("global m1 in")
	c.Set("name", "talon")
	c.Set("user", User{"talon", 25})
	c.Next()
	fmt.Println("global m1 out")
}
func m2(c *gin.Context) {
	fmt.Println("global m2 in")
	c.Next()
	fmt.Println("global m2 out")
}
